<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends InitController {
    public function index(){

	   //解序列化
	   $notice = unserialize($this->opt['notice']);

	   //判断是否超过了试用天数,超过了返回false,没有返回true
	   $judge_member =  judge_member_probation($this->opt,$this->info);
	   // $judge_member =  true;
	  
	   //查询自定义文章库名称
	   $artice_name = M('Options')->where(array('option_name'=>'article_library_name'))->getField('option_value');

	   //会员名
	   $level_name = M('Level')->where(array('level_id'=>$this->info['level']))->getField('level_name');

	   $this->assign(array(			
			'level'=>$this->info['level'],		//赋值等级问题
			'level_name'=>$level_name,			//等级名称
			'user_info'=>$this->info,			//会员详情
			'notice'=>$notice,
			'judge_member'=>$judge_member,
			'artice_name'=>$artice_name,
	   ));
	   $this->display('index');
    }

	//异步上传图片canvas
	public function upload_picture(){
		$img =  I('img');
		$img_type =  I('img_type');
		$img_size =  I('img_size');
		$return_rate = I('return_rate',false);

		//图片限制
		if($img_size > C('upload_size')){
			$this->ajaxReturn(array('超过上传图片大小'));
		}

		//图片格式验证
		if($img_type != 'image/jpeg' and $img_type != 'image/gif' and $img_type != 'image/png' and $img_type != 'image/jpg'){
			$this->ajaxReturn(array('图片格式不对'));
		}

		//图片格式
		switch ($img_type) {
			case 'image/jpeg':
				$s = base64_decode(str_replace('data:image/jpeg;base64,', '', $img));
				$img_type_name = 'jpeg'; 
				break;
			case 'image/gif':
				$s = base64_decode(str_replace('data:image/gif;base64,', '', $img));
				$img_type_name = 'gif';
				break;
			case 'image/png':
				$s = base64_decode(str_replace('data:image/png;base64,', '', $img));
				$img_type_name = 'png';
				break;
			case 'image/jpg':
				$s = base64_decode(str_replace('data:image/jpg;base64,', '', $img));
				$img_type_name = 'jpg';
				break;
			
		}

		$upload         = C('upload_path').'/ad/';//上传路径

		//以上传文件区分文件夹
		$ryear          = date('Y',NOW_TIME);
		$rmonth         = date('m',NOW_TIME);
		$rdate          = date('d',NOW_TIME);

		//获取图片名字
		$picture_name   = random(14).'_'.$this->uid;

		//组装图片
		$picture        = $picture_name.'.'.$img_type_name;

		$upload_path = $upload.$ryear.'/'.$rmonth.'/'.$rdate;
		$upload_path_pictue = $upload_path.'/'.$picture;
		
		mkDirs($upload_path);
		@file_put_contents($upload_path_pictue,$s);

		//图片md5_file
		$md5_file = md5_file("./".$upload_path_pictue."");
		
		if(!empty($md5_file)){
			$result = M('HomePictureAttachment')->where(' `key` = "'.$md5_file.'"')->find();
		}
		if(empty($result)){
			$date = array(
				'img_name'=>$picture_name,
				'upload_time'=>NOW_TIME,
				'picture_address'=>$upload_path_pictue,
				'key'=>$md5_file,
				'user_id'=>$this->uid,
				'is_article'=>0,   //不是微信链接
				'type'=>$img_type_name,      
			);
			D('Article')->picture_attachments($date);
			
		}else{
			//删除图片
			@unlink($upload_path_pictue);
			//获取上传图片图片地址
			$upload_path_pictue = $result['picture_address'];
		}

		//是否返回图片比例，通过return_rate参数来返回，目前只运用到自定义二维码
		if($return_rate){
			//图片信息
			$img_array = getimagesize($upload_path_pictue);
			if(!empty($img_array)){
				//返回比例
				$rate = round(($img_array[1]/$img_array[0]),1);
			}
			$this->ajaxReturn(array('img'=>'/'.$upload_path_pictue,'rate'=>$rate));
		}else{
			$this->ajaxReturn(array('/'.$upload_path_pictue));
		}
	}


	// 使用手册 - 列表
	public function manualList()
	{
		// 获取文章类别列表
		$single_cate_list = D('SingleCate')->getSingleCateList(array('status' => 1));
			// dump($single_cate_list);
		foreach ($single_cate_list as $row) {
			$show_list[$row['cat_id']] = array(
				'cat_id' => $row['cat_id'],
				'cat_name' => $row['cat_name'],
				'cat_status' => $row['status']
			);
		}
		// 获取全部文章
		$single_list = D('Single')->getSingleList(array('status' => 1));
			// dump($single_list);
		foreach ($single_list as $row) {
			$show_list[$row['cat_id']]['single_list'][$row['id']] = $row;
		}

			// dump($show_list);
		$this->assign('show_list', $show_list);

		$this->display('manualList');
	}

	// 使用手册 - 内容展示
	public function manualShow()
	{
		$single_id = I('single_id',0,'intval');

		$single_info = D('Single')->getSingleById($single_id);
			// dump($single_info);
		$this->assign('single_info',$single_info);
		$this->display('manualShow');
	}

	//检验通道内容展示
	public function checkarticle(){
		$this->display('checkarticle');
	}

}
